/*
 * Kelas players menggambarkan urutan antrian dari seluruh pemain
 *
 */

package model.players;
import java.io.Serializable;

public class Players implements Serializable
{
    //-----------------------------ATTRIBUTE--------------------------------
        public Player player[];
        public boolean[] isIsi;
        public boolean arah_maju = true; // maju = 1; mundur = -1
        public int jum_Player;

	//----------------------------CONSTRUCTOR-------------------------------
	public Players(int jum_Player)
	{
            this.player = new Player[jum_Player+1];
            for (int i = 1; i<= jum_Player; i++)
            {
                player[i] = new Player();
                this.isIsi  = new boolean [jum_Player+1];
            }
            
            this.jum_Player = jum_Player;
	}

	//-------------------------------GETTER---------------------------------
//MENGAMBIL Player
        public Player getPlayer(int i)
        {
            return this.player[i];
        }

//MENGAMBIL JUMLAH Player
        public int getJum_Player ()
        {
            return this.jum_Player;
        }

//MENGAMBIL URUTAN PEMAIN
        public int getUrutan(Player P)
        {
            int retval = 0;
            int i = 1;
            while ((i<= jum_Player)&&(!player[i].equals(P)))
            {
                i++;
            }
            if (player[i].equals(P))
            {
                retval = i;
            }
            return retval;
        }

//MENGAMBIL NILAI URUTAN SETELAH Player
        public int urutan_nextPlayer(int urutan_current_plyr)
        {
            if (this.arah_maju)
            {
                if (urutan_current_plyr == this.jum_Player)
                    return 1;
                else
                    return (urutan_current_plyr+1);
            }
            else //reverse
            {
                if (urutan_current_plyr == 1)
                    return (this.jum_Player);
                else
                    return (urutan_current_plyr-1);
            }
        }

//MENGAMBIL NILAI URUTAN SEBELUM Player
        public int urutan_lastPlayer(int urutan_current_plyr)
        {
            if (!this.arah_maju)
            {
                if (urutan_current_plyr == this.jum_Player)
                    return 1;
                else
                    return (urutan_current_plyr+1);
            }
            else //reverse
            {
                if (urutan_current_plyr == 1)
                   return (this.jum_Player);
                else
                    return (urutan_current_plyr-1);
            }
        }


        //----------------------------METHOD------------------------------------

//MENAMBAH ELEMEN DI QUEUE
        public boolean add (Player newP)
        {
            this.player[newP.getNo()] = newP;
            this.isIsi[newP.getNo()] = true;
            return true;
        }

 //MENCETAK ISI QUEUE
        public String PrintQueue(int x) //x buat apa
        {
            String retval = new String ("");
            for (int i = 1; i <= (this.jum_Player); i++)
            {
                retval = retval.concat(i+") "+this.player[i].getNama()+"\n");
                retval = retval.concat(  "  Fund  :"+this.player[i].getUang()+"\n");
                retval = retval.concat(  "  Fun   :"+this.player[i].getFun()+"\n");
                retval = retval.concat(  "  Hunger:"+this.player[i].getHunger()+"\n\n");
            }
            return retval;
        }


//===============================================================================
    //MAIN
    public static void main (String [] args)
    {
        System.out.println("Main untuk kelas Queue Player");
        Player P1 = new Player (1, "Lisa", new String ("satu"),true);
        Player P2 = new Player (2, "Karol", new String ("dua"),true);
        Player P3 = new Player (3, "Vania", new String ("tiga"),true);
        Player P4 = new Player (4, "Pram", new String ("empat"),true);

        Players Q = new Players(4);

        //TEST ADD
        if(Q.add(P1))
            System.out.println("Penambahan Player berhasil");
        Q.add(P2);
        Q.add(P3);
        Q.add(P4);
        System.out.println(Q.PrintQueue(0));

        //TEST JUMLAH Player
        System.out.println("Jumlah Player:"+Q.getJum_Player());
        //TEST GET Player
        System.out.println("Player ke-2:"+Q.getPlayer(2).getNama());
        //TEST URUTAN LAST Player
        System.out.println("Player sebelum Player kedua:"+Q.player[Q.urutan_lastPlayer(2)].getNama());
        //TEST URUTAN NEXT Player
        System.out.println("Player setelah Player kedua:"+Q.player[Q.urutan_nextPlayer(2)].getNama());     

    }
}
